package net.projects.pro5_rss.Buffer;

import net.projects.pro5_rss.bean.CommonInformationItem;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingDeque;

public class NewsBuffer {
    //存储新闻的阻塞队列
    private LinkedBlockingDeque<CommonInformationItem> buffer;
    //用于去重的map
    private ConcurrentHashMap<String,String> storedItem;

    public NewsBuffer() {
        this.buffer = new LinkedBlockingDeque<>();
        this.storedItem = new ConcurrentHashMap<>();
    }

    public void addItem(CommonInformationItem item){
        if (storedItem.containsKey(item.getId())){
            return;
        }
        buffer.add(item);
        storedItem.put(item.getId(),item.getTitle());
    }
    public CommonInformationItem get(){
        try {
            return buffer.take();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
